import modules.net.pynet
import time
import modules.db
html_nextBlockHash=["TipBlk",]
def getTimeFromtimeStamp(timeStamp):
    if isinstance(timeStamp,str):
        timeStamp=float(timeStamp)
    if isinstance(timeStamp,float):
        timeArray = time.localtime(timeStamp)
        return time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
    else:
        return ""

def getNextBlockHash(nextheight,blkhash):
    """
    try:
         blksetByHeightlist = modules.db.CDb.getValueByKey("chainIndexFile",f'blksetByHeight{nextheight}').split(",")
    except KeyError:
        return " "
    else:
        for blockhashset in blksetByHeightlist:
            if blockhashset.split(":")[1] == blkhash:
                return blockhashset.split(":")[0]
    return " "
    """
    blksetByHeightlist = modules.db.CDb.getValueByKey("chainIndexFile",f'blksetByHeight{nextheight}')
    if blksetByHeightlist !=None:
        blksetByHeightlist = blksetByHeightlist.split(",")
        for blockhashset in blksetByHeightlist:
            if blockhashset.split(":")[1] == blkhash:
                return blockhashset.split(":")[0]
        return " "
    else:
        return " "




    

def setNextBlockHash(value):
    html_nextBlockHash.append(value)

modules.net.pynet.CP2pnet.severapp.add_template_global(getTimeFromtimeStamp,'getTimeFromtimeStamp')
modules.net.pynet.CP2pnet.severapp.add_template_global(getNextBlockHash,'getNextBlockHash')
modules.net.pynet.CP2pnet.severapp.add_template_global(setNextBlockHash,'setNextBlockHash')